package com.jxud.controller;

import com.jxud.pojo.Emp;
import com.jxud.pojo.PageBean;
import com.jxud.pojo.Result;
import com.jxud.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

@RestController
@Slf4j
public class EmpController {

    @Autowired
    private EmpService empService;

    @GetMapping("/emps")
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Boolean gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("分页查询参数{},{},{},{},{},{}",page,pageSize,name,gender,begin,end);
        PageBean pageBean  =empService.page(page,pageSize,name,gender,begin,end);
        return Result.success(pageBean);
    }

    @DeleteMapping("/emps/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("删除员工{}",id);
        empService.delete(id);
        return Result.success();
    }

    @PostMapping("/emps")
    public Result add(@RequestBody Emp emp){
        log.info("新增员工{}",emp);
        empService.add(emp);
        return Result.success();
    }

    @GetMapping("/emps/{id}")
    public Result get(@PathVariable Integer id){
        log.info("根据id查询员工{}",id);
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }

    @PutMapping("/emps")
    public Result update(@RequestBody Emp emp){
        log.info("更新员工信息：{}",emp);
        empService.update(emp);
        return Result.success();
    }


}
